package calin.podcast;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

/**
 * Simple podcasts database access helper class. Defines the basic CRUD
 * operations for the calin podcast, and gives the ability to list all podcasts
 * as well as retrieve or modify a specific podcast.
 * 
 * This has been improved from the first version of this tutorial through the
 * addition of better error handling and also using returning a Cursor instead
 * of using a collection of inner classes (which is less scalable and not
 * recommended).
 */
public class CalinPodcastSettingsDbAdapter{

	public static final String KEY_NAME = "name";
	public static final String KEY_VALUE = "value";
	public static final String KEY_ROWID = "_id";
	private static final String DATABASE_TABLE = "settings";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private final Context mCtx;

	private static class DatabaseHelper extends CalinDatabaseHelper {
		DatabaseHelper(Context context) {
			super(context);
		}
	}

	/**
	 * Constructor - takes the context to allow the database to be
	 * opened/created
	 * 
	 * @param ctx
	 *            the Context within which to work
	 */
	public CalinPodcastSettingsDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	/**
	 * Open the podcasts database. If it cannot be opened, try to create a new
	 * instance of the database. If it cannot be created, throw an exception to
	 * signal the failure
	 * 
	 * @return this (self reference, allowing this to be chained in an
	 *         initialization call)
	 * @throws SQLException
	 *             if the database could be neither opened or created
	 */
	public CalinPodcastSettingsDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	/**
	 * Return a Cursor positioned at the podcast that matches the given rowId
	 * 
	 * @param rowId
	 *            id of podcast to retrieve
	 * @return Cursor positioned to matching podcast, if found
	 * @throws SQLException
	 *             if podcast could not be found/retrieved
	 */
	public Cursor fetchItem(String name) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_VALUE }, KEY_NAME + "='" + name + "'", null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	/**
	 * Update the podcast using the details provided. The podcast to be updated
	 * is specified using the rowId, and it is altered to use the title and url
	 * values passed in
	 * 
	 * @param rowId
	 *            id of podcast to update
	 * @param title
	 *            value to set podcast title to
	 * @param url
	 *            value to set podcast url to
	 * @return true if the podcast was successfully updated, false otherwise
	 */
	public boolean updateItem(String name, String value) {
		ContentValues args = new ContentValues();
		args.put(KEY_VALUE, value);

		return mDb.update(DATABASE_TABLE, args, KEY_NAME + "='" + name + "'", null) > 0;
	}
	
	public boolean addItem(String name, String value) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_NAME, name);
		initialValues.put(KEY_VALUE, value);

		return mDb.insert(DATABASE_TABLE, null, initialValues)> 0;
	}
}
